﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Server.Share;
using Server.Models;
using MessageModel;
using Newtonsoft.Json;

namespace Server.Controllers.Client
{
    [RoutePrefix("51API/Invoice")]
    public class InvoiceController : ApiController
    {
        CShare comm = new CShare();

        /// <summary>
        /// 3.1税控信息查询
        /// </summary>
        /// <param name="dc">SKQueryRequestModel,Token</param>
        /// <returns></returns>
        [Route("GetTaxInfo")]
        public IHttpActionResult PostGetTaxInfo([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Token"].ToString());
                SKQueryRequestModel tax = JsonConvert.DeserializeObject<SKQueryRequestModel>(dc["obj"].ToString());
                SKQueryDataModel taxret = comm.iCtrl.GetTaxInfo(tax, comm.uinfo);
                RetModel ret = new RetModel
                {
                    Data = taxret,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Invoice001", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 3.2发票库存查询
        /// </summary>
        /// <param name="dc">TaxCardInvoiceStockQueryModel,Token</param>
        /// <returns></returns>
        [Route("GetTaxKC")]
        public IHttpActionResult PostGetTaxKC([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Token"].ToString());
                TaxCardInvoiceStockQueryModel tax = JsonConvert.DeserializeObject<TaxCardInvoiceStockQueryModel>(dc["obj"].ToString());
                TaxCardInvoiceStockQueryReturnModel taxret = comm.iCtrl.GetTaxKC(tax, comm.uinfo);
                RetModel ret = new RetModel
                {
                    Data = taxret,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Invoice002", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 3.3发票开具
        /// </summary>
        /// <param name="dc">InvoiceIssueModel,Token</param>
        /// <returns></returns>
        [Route("InvoiceInfo")]
        public IHttpActionResult PostInvoiceInfo([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Token"].ToString());
                InvoiceIssueModel tax = JsonConvert.DeserializeObject<InvoiceIssueModel>(dc["obj"].ToString());
                InvoiceReturnModel taxret = comm.iCtrl.InvoiceInfo(tax, comm.uinfo);
                RetModel ret = new RetModel
                {
                    Data = taxret,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Invoice003", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 3.4.金税盘发票查询
        /// </summary>
        /// <param name="dc">InvoiceIssueModel,Token</param>
        /// <returns></returns>
        [Route("GetFP")]
        public IHttpActionResult PostGetFP([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Token"].ToString());
                InvoiceQueryModel tax = JsonConvert.DeserializeObject<InvoiceQueryModel>(dc["obj"].ToString());
                InvoiceQueryReturn taxret = comm.iCtrl.GetFP(tax, comm.uinfo);
                RetModel ret = new RetModel
                {
                    Data = taxret,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Invoice004", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 3.5税收分类编码
        /// </summary>
        /// <param name="dc">TaxCodingMessageModel,Token</param>
        /// <returns></returns>
        [Route("DownLoadTaxCode")]
        public IHttpActionResult PostDownLoadTaxCode([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Toekn"].ToString());
                TaxCodingMessageModel tax = JsonConvert.DeserializeObject<TaxCodingMessageModel>(dc["obj"].ToString());
                TaxCodingReturn taxret = comm.iCtrl.DownLoadTaxCode(tax, comm.uinfo);
                RetModel ret = new RetModel
                {
                    Data = taxret,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Invoice005", ErrInfo = ex.Message, Data = null });
            }
        }
    }
}